package com.smart.manager.weixin.mp.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.ArcoTips;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.common.entities.core.Page;
import com.smart.manager.weixin.mp.entities.bodys.RWeixnMpAccount;
import com.smart.manager.weixin.mp.entities.models.WeixinMpAccount;
import com.smart.manager.weixin.mp.repository.WxmpAccountRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;


/**
 * @author Administrator
 */

@Api(tags = "公众号接入配置")
@RestController
@RequestMapping(SmartConstant.MANAGER_API_PREFIX + "weixin/mp/account")
@RequiredArgsConstructor
public class WxmpAccountApi {

    private final WxmpAccountRepository wxmpAccountRepository;


    @GetMapping(value = "pageList")
    @ApiOperation(value = "查询公众号")
    @SaCheckPermission("wxmp:account:pageList")
    public AjaxResult<Page<WeixinMpAccount>> pageList(@Validated RWeixnMpAccount.SearchBody body) {
        Page<WeixinMpAccount> result = wxmpAccountRepository.pageList(body);
        return AjaxResult.data(result);
    }


    @PostMapping(value = "add")
    @ApiOperation(value = "新增公众号")
    @SaCheckPermission("wxmp:account:add")
    public AjaxResult<String> add(@RequestBody @Validated RWeixnMpAccount.EditBody body) {
        wxmpAccountRepository.add(body);
        return AjaxResult.success();
    }


    @PutMapping(value = "update/{id}")
    @ApiOperation(value = "修改公众号")
    @SaCheckPermission("wxmp:account:update")
    public AjaxResult<String> update(@PathVariable Long id, @RequestBody @Validated RWeixnMpAccount.EditBody body) {
        wxmpAccountRepository.update(id, body);
        return AjaxResult.success();
    }


    @DeleteMapping(value = "delete")
    @ApiOperation(value = "删除公众号")
    @SaCheckPermission("wxmp:account:delete")
    public AjaxResult<Integer> delete(@RequestBody @NotEmpty(message = ArcoTips.PK_EMPTY) List<Long> ids) {
        int x = wxmpAccountRepository.delByIds(ids);
        return AjaxResult.delSuccess(x);
    }

}
